package cn.tradewin.advisor.security.factory;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.services.PipelineBuilder;
import org.slf4j.Logger;
import org.tynamo.security.SecuritySymbols;
import org.tynamo.security.internal.services.LoginContextService;
import org.tynamo.security.services.impl.SecurityFilterChainFactoryImpl;
import org.tynamo.security.shiro.AccessControlFilter;
import org.tynamo.security.shiro.authz.PermissionsAuthorizationFilter;
import org.tynamo.security.shiro.authz.PortFilter;
import org.tynamo.security.shiro.authz.RolesAuthorizationFilter;

public class AdvisorSecurityFilterChainFactory extends SecurityFilterChainFactoryImpl {

	private final LoginContextService loginContextService;

	public AdvisorSecurityFilterChainFactory(
			Logger logger,
			PipelineBuilder builder,
			LoginContextService loginContextService,
			@Inject @Symbol(SymbolConstants.TAPESTRY_VERSION) String tapestryVersion,
			@Inject @Symbol(SecuritySymbols.SUCCESS_URL) String successUrl,
			@Inject @Symbol(SecuritySymbols.LOGIN_URL) String loginUrl,
			@Inject @Symbol(SecuritySymbols.UNAUTHORIZED_URL) String unauthorizedUrl) {
		super(logger, builder, loginContextService, tapestryVersion,
				successUrl, loginUrl, unauthorizedUrl);
		this.loginContextService = loginContextService;
	}

	@Override
	public RolesAuthorizationFilter roles() {
		String name = "roles";
		RolesAuthorizationFilter filter = new RolesAuthorizationFilter(loginContextService);
		filter.setName(name);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		return filter;
	}

	@Override
	public PermissionsAuthorizationFilter perms() {
		String name = "perms";
		PermissionsAuthorizationFilter filter = new PermissionsAuthorizationFilter(
				loginContextService);
		filter.setName(name);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		return filter;
	}

	@Override
	public PortFilter port() {
		PortFilter filter = new PortFilter(loginContextService);
		filter.setName("port");
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		return filter;
	}
}